package STUDENT.TRINHQUANGXUANDUC.OPPS.QuanLyNhanSu.Views;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import STUDENT.TRINHQUANGXUANDUC.OPPS.QuanLyNhanSu.Controllers.Controllers;
import STUDENT.TRINHQUANGXUANDUC.OPPS.QuanLyNhanSu.Entry.Chamcong;
import STUDENT.TRINHQUANGXUANDUC.OPPS.QuanLyNhanSu.Entry.Nhanvien;
import STUDENT.TRINHQUANGXUANDUC.OPPS.QuanLyNhanSu.Entry.Phongban;

public class ViewChamcong extends JFrame {

	private JPanel contentPane;
	private JTextField txtsongaylamviec;
	JTextArea tachamcong;

	Controllers nscControllers = new Controllers();
	List<Nhanvien> listNhanvien;
	List<Phongban> listPhongbans;
	Nhanvien nhanvien;
	Phongban phongban;
	JComboBox cbbnhanvien;
	JComboBox cbbphongban;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ViewChamcong frame = new ViewChamcong();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ViewChamcong() {
		nscControllers.loaddata();
		listNhanvien = nscControllers.getdanhsachNhanvien();
		listPhongbans = nscControllers.getDanhSachPhongBan();
		setTitle("Cham cong");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 438, 335);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel lblMaNhanVien = new JLabel("Ma nhan vien");

		JLabel lblMaPhongBan = new JLabel("Ma phong ban");

		JLabel lblSoNgayLam = new JLabel("So ngay lam viec");

		cbbnhanvien = new JComboBox();
		/*
		 * cbbnhanvien.addItemListener(new ItemListener() { public void
		 * itemStateChanged(ItemEvent arg0) { nhanvien = (Nhanvien)
		 * cbbnhanvien.getSelectedItem();
		 * 
		 * } });
		 */

		cbbphongban = new JComboBox();

		txtsongaylamviec = new JTextField();
		txtsongaylamviec.setColumns(10);

		JButton btnThem = new JButton("Them");
		btnThem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				Chamcong cc = new Chamcong();

				String idnhanvien = cbbnhanvien.getSelectedItem().toString();
				String[] nvtemp = idnhanvien.split("-");
				int idnhv = Integer.parseInt(nvtemp[0]);
				String idphongban = cbbphongban.getSelectedItem().toString();
				String[] tempb = idphongban.split("-");
				int idpb = Integer.parseInt(tempb[0]);

				for (Nhanvien nhanvien : listNhanvien) {
					if (nhanvien.getIdnhanvien() == idnhv) {
						cc.setNhanvien(nhanvien);
					}
				}
				for (Phongban phongban : listPhongbans) {
					if (phongban.getIdphongban() == idpb) {
						cc.setPhongban(phongban);
					}
				}

				cc.setSongaylamviec(Integer.parseInt(txtsongaylamviec.getText()));
				nscControllers.addChamcong(cc);
				tachamcong.setText(nscControllers.getListChamcong());

			}
		});
		tachamcong = new JTextArea();
		tachamcong.setColumns(5);
		tachamcong.setRows(3);
		tachamcong.setText(nscControllers.getListChamcong());

		JScrollPane scr_chamcong = new JScrollPane(tachamcong);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addGroup(gl_contentPane
				.createSequentialGroup()
				.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(scr_chamcong, GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE).addGroup(
								gl_contentPane.createSequentialGroup()
										.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
												.addComponent(lblMaNhanVien).addComponent(lblMaPhongBan).addComponent(
														lblSoNgayLam))
										.addGap(2)
										.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
												.addComponent(btnThem)
												.addComponent(cbbphongban, 0, 320, Short.MAX_VALUE)
												.addComponent(cbbnhanvien, 0, 320, Short.MAX_VALUE)
												.addComponent(txtsongaylamviec, GroupLayout.DEFAULT_SIZE, 320,
														Short.MAX_VALUE))))
				.addContainerGap()));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup().addContainerGap()
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblMaNhanVien)
								.addComponent(cbbnhanvien, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblMaPhongBan)
								.addComponent(cbbphongban, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblSoNgayLam)
								.addComponent(txtsongaylamviec, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(btnThem).addGap(18)
						.addComponent(scr_chamcong, GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);
		initVlueComboBox();
	}

	public void initVlueComboBox() {
		DefaultComboBoxModel nhanvienBoxModel = new DefaultComboBoxModel();

		for (Nhanvien nv : listNhanvien) {
			nhanvienBoxModel.addElement(nv.getIdnhanvien() + "-" + nv.getHoten());
		}

		cbbnhanvien.setModel(nhanvienBoxModel);
		DefaultComboBoxModel phongbanBoxModel = new DefaultComboBoxModel();
		for (Phongban pb : listPhongbans) {
			phongbanBoxModel.addElement(pb.getIdphongban() + "-" + pb.getTenphong());
		}
		cbbphongban.setModel(phongbanBoxModel);

	}

}
